/*
  yarn add gulp gulp-ssh
 */
const gulp = require('gulp')
const GulpSSH = require('gulp-ssh')

const remotePath = `/home/wwwroot/default/hyan_weixin/public/pages/h5/2018/${process.env.npm_package_name}/app`
const config = {
  ssh: { // 正式
    host: '101.37.70.47',
    port: 22,
    username: 'root',
    password: '010.@Flanyanhao'
  },
  // ssh: { // 测试
  //   host: '47.94.231.168',
  //   port: 22,
  //   username: 'root',
  //   password: '@Flan010scmmf5bl',
  // },
  remotePath: remotePath,
  commands: [
    // 删除现有文件
    `rm -rf ${remotePath}`
  ]
}
let gulpSSH = new GulpSSH({
  ignoreErrors: false,
  sshConfig: config.ssh
})
/**
 * 上传前先删除服务器上现有文件...
 */
gulp.task('execSSH', () => {
  console.log('删除服务器上现有文件...')
  return gulpSSH.shell(config.commands, {filePath: 'commands.log'})
      .pipe(gulp.dest('logs'))
})
/**
 * 上传文件到服务器
 */
gulp.task('deploy', ['execSSH'], () => {
  console.log('2s后开始上传文件到服务器...')
  setTimeout(() => {
    return gulp.src(['./dist/**', '!./dist/static/**'])
        .pipe(gulpSSH.dest(config.remotePath))
  }, 2000)
})
